const file = module.exports;
const aliOssPolicyService = require('../../../services/biz/common/aliOssPolicyService');
const log4js = require('../../../utils/log4js');
const configure = require('little-man-config');
const storage_config = configure.get('storage');

file.get_uploadPolicy = (req, res) => {
    aliOssPolicyService.getUploadPolicyWithCallback().then((data) => {
        res.jsonWrap(data);
    }).catch((e) => {
        log4js.error(e);
        res.jsonWrap(null, 1, e.message);
    });
};

file.get_uploadFileKey = async (req, res) => {
    let filename = req.query.filename;
    if (!filename) {
        return res.jsonWrap(null, 1, '无效文件名');
    }
    aliOssPolicyService.getFileKey(filename).then((data) => {
        res.jsonWrap(data);
    }).catch((e) => {
        log4js.error(e);
        res.jsonWrap(null, 1, e.message);
    });
};

file.post_uploadCallback = async (req, res) => {
    let objKey = req.body.filename;
    try {
        log4js.debug(req.body);
        return res.jsonWrap(`${storage_config.oss.host}${objKey}`);
    } catch (e) {
        log4js.error(e);
        return res.jsonWrap(null, 1, e.message);
    }
};